package cn.gaily.eduucenter.controller;

import cn.gaily.commonutils.JwtUtils;
import cn.gaily.commonutils.R;
import cn.gaily.commonutils.UcenterMemberOrder;
import cn.gaily.eduucenter.entity.UcenterMember;
import cn.gaily.eduucenter.entity.vo.RegisterVo;
import cn.gaily.eduucenter.service.UcenterMemberService;
import cn.gaily.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/eduucenter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
        String token = ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        if(StringUtils.isEmpty(registerVo.getMobile()) || StringUtils.isEmpty(registerVo.getPassword())){
            throw new GuliException(20001, "未输入手机号或密码");
        }
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R token(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    @ApiOperation("根据token查询用户信息给订单")
    @GetMapping("getMemberInfoToOrder")
    public UcenterMemberOrder tokenToOrder(String memberId){
        //查询数据库根据id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
}

